#include <iostream>
#include <string>
#include <map>
#include <utility>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int length = s.size();
        int slow = 0;
        int fast = 1;
        map<char, bool> strMap;
        int maxLen = 0;
        
        if (length == 1){
            return 1;
        }

        strMap.insert(make_pair(s[slow], true));
        map<char, bool>::iterator it;
        while (slow < fast && fast < length) {
            it = strMap.find(s[fast]);
            while (it != strMap.end()) {
                strMap.erase(s[slow]);
                slow++;
                it = strMap.find(s[fast]);
            }
            int curLen = fast - slow + 1;
            if (curLen > maxLen){
                maxLen = curLen;
            }
            strMap.insert(make_pair(s[fast], true));
            fast++;
        }
        
        return maxLen;
    }
};
void testcase01(){
    string input = "abcabcbb";
    int want = 3;
    
    Solution so;
    int got = so.lengthOfLongestSubstring(input);
    
    if (got != want){
        printf("want:%d, but got:%d\n", want, got);
    }else {
        printf("success\n");
    }
}

void testcase02(){
    string input = "pwwkew";
    int want = 3;
    
    Solution so;
    int got = so.lengthOfLongestSubstring(input);
    
    if (got != want){
        printf("want:%d, but got:%d\n", want, got);
    }else {
        printf("success\n");
    }
}

void testcase03(){
    string input = "bbbb";
    int want = 1;
    
    Solution so;
    int got = so.lengthOfLongestSubstring(input);
    
    if (got != want){
        printf("want:%d, but got:%d\n", want, got);
    }else {
        printf("success\n");
    }
}
void testcase04(){
    string input = " ";
    int want = 1;
    
    Solution so;
    int got = so.lengthOfLongestSubstring(input);
    
    if (got != want){
        printf("want:%d, but got:%d\n", want, got);
    }else {
        printf("success\n");
    }
}
int main(){
    testcase01();
    testcase02();
    testcase03();
    testcase04();
    return 0;
}